import { combineReducers } from 'redux';
import {
  REGISTER_SUCCESS, REGISTER_FAIL,
  LOGIN_SUCCESS, LOGIN_FAIL,
  ADD_NEW_TASK_SUCCESS, ADD_NEW_TASK_FAIL,
  GET_DAY_TASK_SUCCESS, GET_DAY_TASK_FAIL,
  GET_TASK_DATE_LIST_SUCCESS, GET_TASK_DATE_LIST_FAIL,
  GET_ALL_TASK_SUCCESS, GET_ALL_TASK_FAIL,
  CHANGE_TASK_STATE_SUCCESS, CHANGE_TASK_STATE_FAIL,
} from '../action/actionTypes';

// 注册
const init_register_state = {
  msg: '',
}
const register_reducer = (state=init_register_state, action) => {
  switch(action.type) {
    case REGISTER_SUCCESS:
      return {
        ...state,
        msg: action.data,
      };
    case REGISTER_FAIL:
      return {
        ...state,
        msg: action.data,
      };
    default:
      return state;
  }
}

// 登陆
const init_login_state = {
  msg: '',
}
const login_reducer = (state = init_login_state, action) => {
  switch(action.type) {
    case LOGIN_SUCCESS:
      return {
        ...state,
        msg: action.data,
      };
    case LOGIN_FAIL:
      return {
        ...state,
        msg: action.data,
      };
    default:
      return state;
  }
}

// 新增任务
const init_add_new_task_state = {
  bool: true, // 默认显示当天任务
}
const add_new_task_reducer = (state = init_add_new_task_state, action) => {
  switch(action.type) {
    case ADD_NEW_TASK_SUCCESS:
      let obj1 = {
        ...state,
        bool: action.data,
      };
      return obj1;
    case ADD_NEW_TASK_FAIL:
      let obj2 = {
        ...state,
        bool: action.data,
      };
      return obj2;
    default:
      return state;
  }
}

// 获取某天任务
const init_get_day_task_state = {
  task: {},
  time: 0,
  msg: '',
}
const get_day_task_reducer = (state = init_get_day_task_state, action) => {
  switch(action.type) {
    case GET_DAY_TASK_SUCCESS:
      return {
        ...state,
        task: action.data,
        time: action.time,
      };
    case GET_DAY_TASK_FAIL:
      return {
        ...state,
        msg: action.data,
      };
    default:
      return state;
  }
}

// 获取任务日期列表
const init_task_date_list_state = {
  dateList: [],
  msg: '',
};
const task_date_list_reducer = (state = init_task_date_list_state, action) => {
  switch(action.type) {
    case GET_TASK_DATE_LIST_SUCCESS:
      return {
        ...state,
        dateList: action.data,
      };
    case GET_TASK_DATE_LIST_FAIL:
      return {
        ...state,
        msg: action.data,
      };
    default:
      return state;
  }
}

// 获取单个用户所有任务
const init_all_task_state = {
  task: [],
  msg: '',
}
const get_all_task_reducer = (state = init_all_task_state, action) => {
  switch(action.type) {
    case GET_ALL_TASK_SUCCESS:
      return {
        ...state,
        task: action.data,
      }
    case GET_ALL_TASK_FAIL:
      return {
        ...state,
        msg: action.data,
      }
    default:
      return state;
  }
}

// 改变任务状态
const init_change_task_state_state = {
  bool: false,
  msg: '',
};
const change_task_state_reducer = (state=init_change_task_state_state, action) => {
  switch(action.type) {
    case CHANGE_TASK_STATE_SUCCESS:
      return {
        ...state,
        bool: action.data,
      }
    case CHANGE_TASK_STATE_FAIL:
      return {
        ...state,
        msg: action.data,
      }
    default:
      return state;
  }
}

const reducers = combineReducers({
  register_reducer,
  login_reducer,
  add_new_task_reducer,
  get_day_task_reducer,
  task_date_list_reducer,
  get_all_task_reducer,
  change_task_state_reducer,
});

export default reducers;